package com.srs.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.action.config.ConfigAction;
import com.srs.model.SystemParameter;
import com.srs.util.HibernateUtil;

public class SystemParameterDao {

	final static Logger logger = LoggerFactory
			.getLogger(SystemParameterDao.class);

	private List<SystemParameter> systemParameterList;
	private SystemParameter systemParameter;

	public void delete(Integer systemParameterId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			systemParameter = (SystemParameter) session.get(
					SystemParameter.class, systemParameterId);
			session.delete(systemParameter);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<SystemParameter> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			systemParameterList = session.createQuery("from SystemParameter")
					.list();
			return systemParameterList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	public SystemParameter get(Integer systemParameterId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session
					.createQuery("from SystemParameter sp  where sp.systemParameterId=:systemParameterId");
			q.setInteger("systemParameterId", systemParameterId);
			return (SystemParameter) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public SystemParameter getBySystemParameterName(String name) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session
					.createQuery("from SystemParameter sp where sp.systemParamterName=:name");
			q.setString("name", name);
			return (SystemParameter) q.uniqueResult();
		} finally {
			session.close();
		}
	}

	public void insert(SystemParameter systemParameter) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(systemParameter);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public void update(SystemParameter systemParameter) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(systemParameter);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		SystemParameterDao dao = new SystemParameterDao();
		List<SystemParameter> systemParameters = dao.list();
		
		// List params in list
		List<String> params = new ArrayList<String>();
		for (SystemParameter systemParameter : systemParameters) {
			params.add(systemParameter.getSystemParameterName());
		}

		for (SystemParameter systemParameter : ConfigAction.PARAMETERS) {
			System.out.println(systemParameter.getSystemParameterName());
			
			if (!params.contains(systemParameter.getSystemParameterName())){
				System.out.println("Inser one param");
				System.out.println(systemParameter);
				dao.insert(systemParameter);
			}
		}
	}
}
